package com.chq.demo.exception;

import com.chq.demo.model.common.BaseResponse;
import com.chq.demo.model.common.ResponseEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 统一异常处理
 *
 * @author CHQ
 */
@ControllerAdvice
public class ExceptionHandle {

    /**
     * 404
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseBody
    public BaseResponse noHandlerFoundHandler(Exception e) {
        e.printStackTrace();
        return BaseResponse.fail(ResponseEnum.NOT_FOUND);
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse handler(Exception e) {
        e.printStackTrace();
        return BaseResponse.fail(ResponseEnum.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public BaseResponse RuntimeException(Exception e) {
        e.printStackTrace();
        return BaseResponse.fail(e.getMessage());
    }

}